import { NextFunction, Request, Response } from "express";
import { checkField, getUserInfo } from "../../utils/common";
import classSer from "./service";

class ClassMidd {
  /**
   * 根据班级命令获取对应的班级id
   * @param req
   * @param res
   * @param next
   */
  async getClassIdFromCommandWord(
    req: Request,
    _res: Response,
    next: NextFunction
  ) {
    const { command_word } = req.body;

    // 缺少参数
    if (!checkField(command_word)) return next({ code: 400, msg: "缺少参数" });

    // 获取对应的id
    const { id } = (
      await classSer.getClassIdFromCommandWord(command_word)
    )[0] as any;

    // 添加到req中去
    (req as any).classId = id;

    next();
  }

  /**
   * 判断班级是否存在了
   * @param req
   * @param _res
   * @param next
   * @returns
   */
  async isClassExist(req: Request, _res: Response, next: NextFunction) {
    const { name } = req.body;
    if (!checkField(name)) return next({ code: 400, msg: "缺少参数" });

    // 判断班级是否创建过了
    if (await classSer.isClassExist(name)) {
      next({ code: 200, msg: "该班级已经创建过了" });
    } else {
      next();
    }
  }

  /**
   * 通过口令判断班级是否存在了
   * @param req
   * @param _res
   * @param next
   * @returns
   */
  async isClassExistFromCommandWord(
    req: Request,
    _res: Response,
    next: NextFunction
  ) {
    const { command_word } = req.body;
    if (!checkField(command_word)) return next({ code: 400, msg: "缺少参数" });

    // 判断班级是否创建过了
    if (await classSer.isClassExistByCommandWord(command_word)) {
      next();
    } else {
      next({ code: 200, msg: "对应的班级不存在" });
    }
  }

  /**
   * 通过班级id判断班级是否存在了
   * @param req
   * @param _res
   * @param next
   * @returns
   */
  async isClassExistFromId(
    req: Request,
    _res: Response,
    next: NextFunction
  ) {
    const { classId } = req.body;
    if (!checkField(classId)) return next({ code: 400, msg: "缺少参数" });

    // 判断班级是否创建过了
    if (await classSer.isClassExistByID(classId)) {
      next();
    } else {
      next({ code: 200, msg: "对应的班级不存在" });
    }
  }

  /**
   * 判断是否重复加入了班级
   * @param req
   * @param _res
   * @param next
   */
  async isRepeatAddClass(req: Request, _res: Response, next: NextFunction) {
    // 获取班级的id
    const classId = (req as any).classId;
    // 用户的id
    const { id } = getUserInfo(req);
    if (await classSer.isRepeatAddClass(classId, id)) {
      // 已经加入过该班级了
      next({ code: 200, msg: "已经加入过该班级了" });
    } else {
      // 还没加入过该班级
      next();
    }
  }

  /**
   * 判断是否加入了班级
   * @param req
   * @param _res
   * @param next
   */
  async isAddClass(req: Request, _res: Response, next: NextFunction) {
    // 获取班级的id
    const { classId, stuId } = req.body;

    if (await classSer.isRepeatAddClass(classId, stuId)) {
      // 已经加入过该班级了
      next();
    } else {
      // 还没加入过该班级
      next({ code: 200, msg: "学生还没加入过该班级" });
    }
  }

  /**
   * 判断用户是否有该班级
   * @param req 
   * @param _res 
   * @param next 
   * @returns 
   */
  async isUserHasClass(req: Request, _res: Response, next: NextFunction) {
    const { classId } = req.body;
    const { id } = getUserInfo(req);
    if (!checkField(classId)) return next({ code: 400, msg: "缺少参数" });
    // 判断班级是否创建过了
    if (await classSer.isUserHasClass(classId, id)) {
      next();
    } else {
      next({ code: 200, msg: "该用户没有该班级" });
    }
  }
}
export default new ClassMidd();
